package start;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Day14 {

    public static void main(String[] args) {
        SpuerMan spuerMan = new SpuerMan();
        Human proxyHuman = (Human) ProxyFactory.getProxyInstance(spuerMan);
        System.out.println(proxyHuman.getEat());
    }

}
interface Human {
    public String getEat();
}
class SpuerMan implements Human {
    @Override
    public String getEat() {
        System.out.println("SpuerMan-> eat");
        return "I can eat";
    }
}
class  ProxyFactory {
    public static Object getProxyInstance(Object obj) {
        MyHandler myHandler = new MyHandler(obj);
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), myHandler);
    }
}
class MyHandler implements InvocationHandler {

    private Object object;

    public MyHandler() {
    }

    public MyHandler(Object object) {
        this. object = object;
    }

    //将被代理类要执行的方法功能声明到incoke()中
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before>>>");
        Object returnValue =  method.invoke(object, args);
        System.out.println("after>>>");
        return returnValue;
    }
}